package de.fuberlin.inf.alp4.ue1;

import javax.activity.InvalidActivityException;

import de.fuberlin.inf.utils.*;

public class Test {

    public static void main(String[] args) throws InvalidActivityException {
        int nr = 0;
        if (args.length > 0)
            try {
                perform(Integer.parseInt(args[0]));
            }
            catch (NumberFormatException exc) {}
        
        if (nr == 0) {
            int userInput = -1;
            while (true) {
                System.out.println("Please specify the number of integers.");
                System.out.println("  1. 4,000");
                System.out.println("  2. 40,000");
                System.out.println("  3. 400,000");
                System.out.println("  4. 4,000,000");
                System.out.println("  5. 40,000,000");
                System.out.println("  6. custom value");
                System.out.println("\n  0. Exit application");
                userInput = ConsoleInput.readInt("Your choice?", "Not a number!");
                switch (userInput) {
                case 0:
                    System.out.println("Bye...");
                    System.exit(0);
                case 1:
                    perform(4000);
                    break;
                case 2:
                    perform(40000);
                    break;
                case 3:
                    perform(400000);
                    break;
                case 4:
                    perform(4000000);
                    break;
                case 5:
                    perform(40000000);
                    break;
                case 6:
                    perform(ConsoleInput.readInt("Number of integers?", "Not a number!"));
                    break;
                default:
                    System.out.println("Please choose from available options.");
                }
            }
        }
    }

    private static void perform(int nr) throws InvalidActivityException {
        long[] arr = (TestData.randomNumbersArray(nr, 1, 1000000));
        
        ConsoleOutput.show(arr);
        Timer timer = new Timer();
        timer.start();
        ConsoleOutput.show(SequentialMergesort.sort(arr));
        timer.stop();
        System.out.println("sequential: " + timer);
        timer.start();
        ConsoleOutput.show(ConcurrendMergesort.sort(arr));
        timer.stop();
        System.out.println("concurrend: " + timer);
    }
}
